<template>
    <div class="register-container">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span class="register-title">注册</span>
            </div>
            <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="100px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="registerForm.password" placeholder="请输入密码" show-password prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请再次输入密码" show-password prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="el-icon-message"></el-input>
                </el-form-item>
                <el-form-item class="button-group">
                    <el-button type="primary" @click="submitForm('registerForm')" class="register-button">注册</el-button>
                    <el-button @click="resetForm('registerForm')" class="reset-button">重置</el-button>
                    <el-button @click="goToLogin" class="login-button">登录</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import axios from 'axios';

export default {
    data() {
        const validateConfirmPassword = (rule, value, callback) => {
            if (value !== this.registerForm.password) {
                callback(new Error('两次输入的密码不一致'));
            } else {
                callback();
            }
        };
        return {
            registerForm: {
                username: '',
                password: '',
                confirmPassword: '',
                email: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请再次输入密码', trigger: 'blur' },
                    { validator: validateConfirmPassword, trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        async submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    try {
                        const res = await axios.post('http://localhost:3000/api/auth/indexSign', this.registerForm);
                        localStorage.setItem('token', res.data.token);
                        this.$message.success('注册成功');
                        this.$router.push('/sellBooks');
                    } catch (err) {
                        this.$message.error('注册失败，请检查输入信息');
                    }
                } else {
                    console.log('提交失败!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        goToLogin() {
            this.$router.push('/indexLogin');
        }
    }
};
</script>

<style scoped>
.register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background: linear-gradient(135deg, #6a11cb, #2575fc);
    animation: fadeIn 1s ease-in-out;
}

.box-card {
    width: 400px;
    border-radius: 15px;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
    background-color: #ffffff;
    animation: slideIn 0.8s ease-in-out;
}

.register-title {
    font-size: 24px;
    font-weight: bold;
    color: #333;
}

.el-form-item__label {
    color: #555;
}

.el-input {
    border-radius: 8px;
}

.button-group {
    display: flex;
    justify-content: space-between;
}

.register-button, .reset-button {
    flex: 1;
    margin-right: 10px;
    border-radius: 8px;
}

.register-button {
    background-color: #6a11cb;
    border-color: #6a11cb;
}

.register-button:hover {
    background-color: #4a00e0;
    border-color: #4a00e0;
}

.reset-button {
    background-color: #f5f5f5;
    border-color: #ddd;
    color: #333;
}

.login-button {
    background-color: #f5f5f5;
    border-color: #ddd;
    color: #333;
}

.reset-button:hover {
    background-color: #ddd;
    border-color: #ccc;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideIn {
    from {
        transform: translateY(-50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}
</style>